<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>用户管理 - 网吧管理系统</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/materialdesignicons.min.css" rel="stylesheet">
    <link href="css/style.min.css" rel="stylesheet">
</head>
<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <aside class="lyear-layout-sidebar">
            <div id="logo" class="sidebar-header">
                <a href="index.jsp"><img src="images/logo-sidebar.png" alt="网吧管理系统" /></a>
            </div>
            <div class="lyear-layout-sidebar-scroll">
                <nav class="sidebar-main">
                    <ul class="nav nav-drawer">
                        <li class="nav-item"><a href="StatisticsServlet"><i class="mdi mdi-home"></i> 后台首页</a></li>
                        <li class="nav-item"><a href="ComputerServlet?action=list"><i class="mdi mdi-laptop"></i> 电脑管理</a></li>
                        <li class="nav-item"><a href="OrderServlet?action=list"><i class="mdi mdi-cart"></i> 订单管理</a></li>
                        <li class="nav-item"><a href="SessionServlet?action=list"><i class="mdi mdi-timer"></i> 上机管理</a></li>
                        <li class="nav-item active"><a href="UserServlet?action=list"><i class="mdi mdi-account"></i> 用户管理</a></li>
                    </ul>
                </nav>
            </div>
        </aside>

        <header class="lyear-layout-header">
            <nav class="navbar navbar-default">
                <div class="topbar">
                    <div class="topbar-left">
                        <div class="lyear-aside-toggler">
                            <span class="lyear-toggler-bar"></span>
                            <span class="lyear-toggler-bar"></span>
                            <span class="lyear-toggler-bar"></span>
                        </div>
                        <span class="navbar-page-title">网吧管理系统</span>
                    </div>
                    <ul class="topbar-right">
                        <li class="dropdown dropdown-profile">
                            <a href="javascript:void(0)" data-toggle="dropdown">
                                <img class="img-avatar img-avatar-48 m-r-10" src="images/users/avatar.jpg" alt="用户头像" />
                                <span>管理员 <span class="caret"></span></span>
                            </a>
                            <ul class="dropdown-menu dropdown-menu-right">
                                <li><a href="change_password.jsp"><i class="mdi mdi-lock-outline"></i> 修改密码</a></li>
                                <li><a href="logout.jsp"><i class="mdi mdi-logout-variant"></i> 退出登录</a></li>
                            </ul>
                        </li>
                    </ul>
                </div>
            </nav>
        </header>

        <main class="lyear-layout-content">
            <div class="container-fluid">
                <h2>用户列表</h2>

                <!-- 操作按钮 -->
                <div class="text-right mb-3">
                    <!-- 新增用户按钮 -->
                    <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#addUserModal">新增用户</button>
                    <!-- 查询用户按钮 -->
                    <button class="btn btn-success btn-sm" data-toggle="modal" data-target="#searchUserModal">查询用户</button>
                    <!-- 刷新按钮 -->
                    <button class="btn btn-secondary btn-sm" onclick="refreshPage()">刷新</button>
                </div>

                <!-- 用户表格 -->
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>邮箱</th>
                        <th>性别</th>
                        <th>账户余额</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <%
                        // 从 request 中获取用户列表
                        List<com.houduan.model.User> users = (List<com.houduan.model.User>) request.getAttribute("users");
                        if (users != null && !users.isEmpty()) {
                            for (com.houduan.model.User user : users) {
                    %>
                    <tr>
                        <td><%= user.getId() %></td>
                        <td><%= user.getUsername() %></td>
                        <td><%= user.getEmail() %></td>
                        <td><%= user.getGender() %></td>
                        <td><%= user.getBalance() %></td>
                        <td>
                            <!-- 编辑用户 -->
                            <button class="btn btn-info btn-sm"
                                    data-toggle="modal"
                                    data-target="#editUserModal"
                                    onclick="populateEditForm('<%= user.getId() %>', '<%= user.getUsername() %>', '<%= user.getEmail() %>', '<%= user.getGender() %>', '<%= user.getBalance() %>', '<%= user.getPhone() %>')">编辑</button>
                            <!-- 删除用户 -->
                            <a href="UserServlet?action=delete&id=<%= user.getId() %>"
                               class="btn btn-danger btn-sm"
                               onclick="return confirm('确认删除此用户吗？')">删除</a>
                        </td>

                    </tr>
                    <%
                        }
                    } else {
                    %>
                    <tr>
                        <td colspan="6" class="text-center">没有用户数据</td>
                    </tr>
                    <% } %>
                    </tbody>
                </table>
            </div>
        </main>

        <jsp:include page="account_management_modals.jsp" />
        <script src="js/jquery.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/scripts.min.js"></script>
        <script>
            // 刷新页面的 JavaScript 函数
            function refreshPage() {
                window.location.href = "UserServlet?action=list"; // 调用后端刷新用户列表
            }
        </script>
</body>
</html>